package com.eztalks.paypal;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.paypal.api.payments.Amount;
import com.paypal.api.payments.CreditCard;
import com.paypal.api.payments.Payee;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.PaymentExecution;
import com.paypal.api.payments.RedirectUrls;
import com.paypal.api.payments.Transaction;
import com.paypal.api.payments.Webhook;
import com.paypal.api.payments.WebhookList;
import com.paypal.base.Constants;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;

public class PayeeTest {
	private static final String clientID = "AfcY59o_imV3VlN4VYpqH30gIfrhZ5WrY4GMyfZFvL94EXG_BQttwRrlkbgrfye6H3zgqFEV3e2brjUy";
	private static final String clientSecret = "EFX2Loz2VE-hdidZ5ktob8Aq4FqJJYFctFJKrhkKbPkLAnnjlkueix4-7MoHfz2V3KhPVTDJSyFMgzSC";

	@Test
	public void createCard() {
		CreditCard creditCard = new CreditCard();
		creditCard.setExpireMonth(11);
		creditCard.setExpireYear(2018);
		creditCard.setNumber("4669424246660779");
		creditCard.setCvv2(0123);
		creditCard.setType("visa");

		try {
			APIContext apiContext = new APIContext(clientID, clientSecret, Constants.SANDBOX);

			CreditCard createdCreditCard = creditCard.create(apiContext);
			System.out.println(creditCard);
			System.out.println(createdCreditCard);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*{
		  "id": "PAY-8S611394TW387750DLPG235Q",
		  "intent": "sale",
		  "payer": {
		    "payment_method": "paypal"
		  },
		  "transactions": [
		    {
		      "related_resources": [],
		      "amount": {
		        "currency": "USD",
		        "total": "10.00"
		      },
		      "payee": {
		        "email": "tom-buyer@fsmeeting.com"
		      }
		    }
		  ],
		  "state": "created",
		  "create_time": "2018-10-22T11:01:10Z",
		  "links": [
		    {
		      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8S611394TW387750DLPG235Q",
		      "rel": "self",
		      "method": "GET"
		    },
		    {
		      "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd\u003d_express-checkout\u0026token\u003dEC-7A9016435F435482A",
		      "rel": "approval_url",
		      "method": "REDIRECT"
		    },
		    {
		      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8S611394TW387750DLPG235Q/execute",
		      "rel": "execute",
		      "method": "POST"
		    }
		  ]
		}*/
	@Test
	public void createPayments() {
		APIContext apiContext = new APIContext(clientID, clientSecret, Constants.SANDBOX);

		Payment payment = new Payment();
		payment.setIntent("sale");

		RedirectUrls redirectUrls = new RedirectUrls();
		redirectUrls.setCancelUrl("http://localhost:3000/cancel");
		redirectUrls.setReturnUrl("http://localhost:3000/return");
		payment.setRedirectUrls(redirectUrls);

		Payer payer = new Payer();
		payer.setPaymentMethod("paypal");
		payment.setPayer(payer);

		Transaction transaction = new Transaction();
		Payee payee = new Payee();
		// payee.setEmail("tom-buyer@fsmeeting.com");
		payee.setEmail("111@fsmeeting.com");
		transaction.setPayee(payee);

		Amount amount = new Amount("AUD", "10");
		transaction.setAmount(amount);
		List<Transaction> transactions = new ArrayList<Transaction>();
		transactions.add(transaction);
		payment.setTransactions(transactions);

		Payment payment2 = null;
		try {
			payment2 = payment.create(apiContext);
			System.out.println(payment2);
		} catch (PayPalRESTException e) {
			e.printStackTrace();
			System.err.println(payment2);
		}
	}
	
	// http://localhost:3000/return?paymentId=PAY-8S611394TW387750DLPG235Q&token=EC-7A9016435F435482A&PayerID=2KLWHMMGREBKJ
	
	/*
	 {
	  "id": "PAY-8S611394TW387750DLPG235Q",
	  "intent": "sale",
	  "payer": {
	    "payment_method": "paypal",
	    "status": "VERIFIED",
	    "payer_info": {
	      "email": "tom-facilitator@fsmeeting.com",
	      "first_name": "test",
	      "last_name": "facilitator",
	      "payer_id": "2KLWHMMGREBKJ",
	      "country_code": "C2",
	      "shipping_address": {
	        "recipient_name": "test facilitator",
	        "line1": "NO 1 Nan Jin Road",
	        "city": "Shanghai",
	        "country_code": "C2",
	        "postal_code": "200000",
	        "state": "Shanghai"
	      }
	    }
	  },
	  "cart": "7A9016435F435482A",
	  "transactions": [
	    {
	      "related_resources": [
	        {
	          "sale": {
	            "id": "6UH76832MM651743V",
	            "amount": {
	              "currency": "USD",
	              "total": "10.00",
	              "details": {
	                "subtotal": "10.00"
	              }
	            },
	            "payment_mode": "INSTANT_TRANSFER",
	            "state": "completed",
	            "protection_eligibility": "ELIGIBLE",
	            "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
	            "transaction_fee": {
	              "currency": "USD",
	              "value": "0.64"
	            },
	            "parent_payment": "PAY-8S611394TW387750DLPG235Q",
	            "create_time": "2018-10-22T11:28:21Z",
	            "update_time": "2018-10-22T11:28:21Z",
	            "links": [
	              {
	                "href": "https://api.sandbox.paypal.com/v1/payments/sale/6UH76832MM651743V",
	                "rel": "self",
	                "method": "GET"
	              },
	              {
	                "href": "https://api.sandbox.paypal.com/v1/payments/sale/6UH76832MM651743V/refund",
	                "rel": "refund",
	                "method": "POST"
	              },
	              {
	                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8S611394TW387750DLPG235Q",
	                "rel": "parent_payment",
	                "method": "GET"
	              }
	            ]
	          }
	        }
	      ],
	      "amount": {
	        "currency": "USD",
	        "total": "10.00",
	        "details": {}
	      },
	      "payee": {
	        "email": "tom-buyer@fsmeeting.com",
	        "merchant_id": "EFMN26TZNWACS"
	      },
	      "item_list": {
	        "shipping_address": {
	          "recipient_name": "facilitator test",
	          "line1": "NO 1 Nan Jin Road",
	          "city": "Shanghai",
	          "country_code": "C2",
	          "postal_code": "200000",
	          "state": "Shanghai"
	        }
	      }
	    }
	  ],
	  "state": "approved",
	  "create_time": "2018-10-22T11:28:21Z",
	  "links": [
	    {
	      "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8S611394TW387750DLPG235Q",
	      "rel": "self",
	      "method": "GET"
	    }
	  ]
	}
	 */
	@Test
	public void excutePaymount() {
		APIContext apiContext = new APIContext(clientID, clientSecret, Constants.SANDBOX);
		Payment payment = new Payment();
		payment.setId("PAYID-LPHNC4Y80J81698YH658523C");
		
		PaymentExecution paymentExecution = new PaymentExecution();
		paymentExecution.setPayerId("2KLWHMMGREBKJ");
		try {
			Payment execute = payment.execute(apiContext, paymentExecution);
			System.out.println(execute);
		} catch (PayPalRESTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void webhook() {
	}
}
